package com.metroinfo.servers;

import java.net.InetSocketAddress;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.metroinfo.infrastructure.MainDataStructure;
import com.metroinfo.workers.ChannelIO;
import com.metroinfo.workers.RequestServicer;
import com.metroinfo.workers.selector.Acceptor;
import com.metroinfo.workers.selector.DispatcherN;

public class SimpleSelectServer {

	ServerSocketChannel ssc;

	static private int BACKLOG = 1024;

	private final MainDataStructure mainDataStructure;

	public SimpleSelectServer(int port) throws Exception {

		ssc = ServerSocketChannel.open();
		ssc.socket().setReuseAddress(true);
		ssc.socket().bind(new InetSocketAddress(port), BACKLOG);
		// ssc.socket().setSoTimeout(5000);
		mainDataStructure = new MainDataStructure();
	}

	public void runServer() throws Exception {

		DispatcherN d = new DispatcherN();
		Acceptor a = new Acceptor(ssc, d, mainDataStructure);
		new Thread(a).start();
		d.run();
	}

	static public void main(String args[]) throws Exception {

		SimpleServer simpleServer = new SimpleServer(8000);

		System.out.println("startovao se");
		simpleServer.runServer();

	}
}
